/**
 * Copyright 2013 Net355 Co,.Ltd. 
 * All rights reserved.
 *
 * "StaticTextHelper.java is for what: "
 * 
 * Create by Ray at Dec 10, 2013 10:01:46 PM.
 * Ray [email:rayinhangzhou@126.com]
 * 
 * Revision History
 *
 * Date            Programmer                   Notes
 * ---------    ---------------------  -----------------------------------
 * Dec 10, 2013            Ray                       initial
 **/

package com.net355.eblog.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.net355.eblog.models.EbNote;
import com.net355.eblog.models.EbNotebook;
import com.net355.eblog.models.EbTag;

public class StaticHtmlHelper {

	public static String getBookList() {
		StringBuilder html = new StringBuilder();
		html.append("<ul id=\"ul_notebooks\">");
		List<?> cacheNotebookList = (List<?>) CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK);
		for (int i = 0; i < cacheNotebookList.size(); i++) {
			EbNotebook notebook = (EbNotebook) CacheHelper.get_cache((String) cacheNotebookList.get(i));
			html.append("<li><a href=\"/blog/notebook/" + notebook.getGuid() + "\" title=\"更新于"
					+ DateToolkit.formatDate(new Date(notebook.getUpdateTime()), "yyyy-MM-dd HH:mm:ss") + "\">" + notebook.getName() + " ("
					+ notebook.getNotes().size() + ")</a></li>");
		}
		html.append("</ul>");
		return html.toString();
	}

	public static String getSummaryList() {
		StringBuilder html = new StringBuilder();
		html.append("<div>更新次数：<em>" + GlobalParams.COUNT_UPDATE.get() + "</em></div>");
		html.append("<div>访问次数：<em>" + GlobalParams.COUNT_VIEW.incrementAndGet() + "</em></div>");
		html.append("<div>聊天条数：<em>" + GlobalParams.COUNT_CHAT.get() + "</em></div>");
		html.append("<div>搜索次数：<em>" + GlobalParams.COUNT_SEARCH.get() + "</em></div>");
		return html.toString();
	}

	/**
	 * @param notes
	 * @param offset 从0开始
	 * @param size 获取条数
	 * @return
	 */
	public static String getNoteListByNoteGuids(List<String> notes, int offset, int size) {
		StringBuilder html = new StringBuilder();
		int step = 0;
		int total = notes.size();
		int index = total - 1 - offset - step;
		while (step < size && index > -1) {
			EbNote note = (EbNote) CacheHelper.get_cache(notes.get(index));
			html.append("<div class=\"item\">");
			html.append("<div><span>" + DateToolkit.formatDate(new Date(note.getUpdateTime()), "yyyy-MM-dd HH:mm:ss") + "</span><h4>"
					+ note.getTitle() + "</h4></div>");
			html.append("<p><a href=\"/blog/note/" + note.getGuid() + "\">" + note.getContent() + "</a></p>");
			html.append("</div>");
			step++;
			index--;
		}
		return html.toString();
	};
	
	public static ExecuteResult getNoteListByNoteGuidsAndMatch(List<String> notes, String match, int offset, int size) {
		ExecuteResult eResult = new ExecuteResult(true, "", offset);
		StringBuilder html = new StringBuilder();
		int step = 0;
		int total = notes.size();
		int index = total - 1 - offset - step;
		if(StringUtil.isEmpty(match)) {
			while (step < size && index > -1) {
				EbNote note = (EbNote) CacheHelper.get_cache(notes.get(index));
				html.append("<div class=\"item\">");
				html.append("<div><span>" + DateToolkit.formatDate(new Date(note.getUpdateTime()), "yyyy-MM-dd HH:mm:ss") + "</span><h4>"
						+ note.getTitle() + "</h4></div>");
				html.append("<p><a href=\"/blog/note/" + note.getGuid() + "\" target=\"_blank\">" + note.getContent() + "</a></p>");
				html.append("</div>");
				step++;
				index--;
			}
		} else {
			while (step < size && index > -1) {
				EbNote note = (EbNote) CacheHelper.get_cache(notes.get(index));
				int matchIndex_title = note.getTitle().toLowerCase().indexOf(match);
				if(matchIndex_title!=-1) {
					String toReplace = note.getTitle().substring(matchIndex_title, matchIndex_title+match.length());
					html.append("<div class=\"item\">");
					html.append("<div><span>" + DateToolkit.formatDate(new Date(note.getUpdateTime()), "yyyy-MM-dd HH:mm:ss") + "</span><h4>"
							+ note.getTitle().replace(toReplace, "<font style=\"color:#D44; font-weight:bold;\">"+toReplace+"</font>") + "</h4></div>");
					html.append("<p><a href=\"/blog/note/" + note.getGuid() + "\" target=\"_blank\">" 
							+ note.getContent().replace(toReplace, "<font style=\"color:#D44; font-weight:bold;\">"+toReplace+"</font>") + "</a></p>");
					html.append("</div>");
					step++;
					index--;
					continue;
				}
				int matchIndex_content = note.getContent().toLowerCase().indexOf(match);
				if(matchIndex_content!=-1) {
					String toReplace = note.getContent().substring(matchIndex_content, matchIndex_content+match.length());
					html.append("<div class=\"item\">");
					html.append("<div><span>" + DateToolkit.formatDate(new Date(note.getUpdateTime()), "yyyy-MM-dd HH:mm:ss") + "</span><h4>"
							+ note.getTitle() + "</h4></div>");
					html.append("<p><a href=\"/blog/note/" + note.getGuid() + "\" target=\"_blank\">" 
							+ note.getContent().replace(toReplace, "<font style=\"color:#D44; font-weight:bold;\">"+toReplace+"</font>") + "</a></p>");
					html.append("</div>");
					step++;
					index--;
					continue;
				}
				List<?> sectionList = (List<?>)CacheHelper.get_cache_text(notes.get(index));
				for (int i=0; i<sectionList.size(); i++) {
					String section = (String)sectionList.get(i);
					int matchIndex = section.toLowerCase().indexOf(match);
					if(matchIndex!=-1) {
						String toReplace = section.substring(matchIndex, matchIndex+match.length());
						html.append("<div class=\"item\">");
						html.append("<div><span>" + DateToolkit.formatDate(new Date(note.getUpdateTime()), "yyyy-MM-dd HH:mm:ss") + "</span><h4>"
								+ note.getTitle() + "</h4></div>");
						html.append("<p><a href=\"/blog/note/" + note.getGuid() + "\" target=\"_blank\">" 
								+ section.replace(toReplace, "<font style=\"color:#D44; font-weight:bold;\">"+toReplace+"</font>") + "</a></p>");
						html.append("</div>");
						step++;
						break;
					}
				}
				index--;
			}
		}
		eResult.setMessage(html.toString());
		eResult.setTag(total-index-1);
		return eResult;
	};

	public static String getTagListByTagGuids(List<?> tags) {
		StringBuilder html = new StringBuilder();
		for (int i = tags.size()-1; i > -1 ; i--) {
			EbTag tag = (EbTag) CacheHelper.get_cache((String)tags.get(i));
			html.append("<div id=\""+tag.getGuid()+"_"+tag.getNotes().size()+"\"><a href=\"/blog/tag/"+tag.getGuid()+"\">"+tag.getName()+"</a></div>");
		}
		return html.toString();
	}
	
	public static String getTagListByNote(EbNote note) {
		StringBuilder html = new StringBuilder();
		for (int i = 0; i < note.getTags().size(); i++) {
			EbTag tag = (EbTag) CacheHelper.get_cache(note.getTags().get(i));
			html.append("<a href=\"/blog/tag/" + tag.getGuid() + "\" title=\"更新于"
					+ DateToolkit.formatDate(new Date(tag.getUpdateTime()), "yyyy-MM-dd HH:mm:ss") + "\">" + tag.getName() + "("
					+ tag.getNotes().size() + ")</a>");
		}
		return html.toString();
	}
	
	public static String getTagsForKeyByNote(EbNote note) {
		List<String> tags = new ArrayList<>();
		for (int i = 0; i < note.getTags().size(); i++) {
			EbTag tag = (EbTag) CacheHelper.get_cache(note.getTags().get(i));
			tags.add(tag.getName());
		}
		return tags.toString();
	}
	

}
